/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.HandlerHandler;
import control.DBHandler;
import control.Main;

/**
 *
 * @author Morten J, Morten B & Jonas
 */
public class DBLogin extends javax.swing.JFrame {

    private GUI g;

    /**
     * Creates new form DBLog
     */
    public DBLogin() {
        initComponents();
        setLocation(150, 100);
        setResizable(false);
        jPass.requestFocus();
        jPass.selectAll();
    }

    private void login() {
        Main.setDbConnection(new DBHandler(jHost.getText(), jPort.getText(), jName.getText(), jUser.getText(), jPass.getText(), jLabError));
        if (Main.getDbConnection().isConnected()) {
            g = new GUI();
            HandlerHandler hh = new HandlerHandler();
            setVisible(false);
            g.setVisible(true);
            g.setLocation(50, 50);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanPageHolder = new javax.swing.JPanel();
        jHost = new javax.swing.JTextField();
        jPort = new javax.swing.JTextField();
        jHostPortLabel = new javax.swing.JLabel();
        jLabSlash = new javax.swing.JLabel();
        jName = new javax.swing.JTextField();
        jNameLabel = new javax.swing.JLabel();
        jUser = new javax.swing.JTextField();
        jPass = new javax.swing.JTextField();
        jUserLabel = new javax.swing.JLabel();
        jPassLabel = new javax.swing.JLabel();
        jTitle = new javax.swing.JLabel();
        jBtnDBLogin = new javax.swing.JButton();
        jLabError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanPageHolder.setBackground(new java.awt.Color(186, 153, 108));

        jHost.setBackground(java.awt.Color.darkGray);
        jHost.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jHost.setForeground(new java.awt.Color(250, 250, 250));
        jHost.setText("localhost");
        jHost.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));

        jPort.setBackground(java.awt.Color.darkGray);
        jPort.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jPort.setForeground(new java.awt.Color(250, 250, 250));
        jPort.setText("3306");
        jPort.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));

        jHostPortLabel.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jHostPortLabel.setForeground(new java.awt.Color(250, 250, 250));
        jHostPortLabel.setText("Host / Port:");

        jLabSlash.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabSlash.setForeground(new java.awt.Color(250, 250, 250));
        jLabSlash.setText("/");

        jName.setBackground(java.awt.Color.darkGray);
        jName.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jName.setForeground(new java.awt.Color(250, 250, 250));
        jName.setText("MuseumSyd");
        jName.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));

        jNameLabel.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jNameLabel.setForeground(new java.awt.Color(250, 250, 250));
        jNameLabel.setText("Databasenavn:");

        jUser.setBackground(java.awt.Color.darkGray);
        jUser.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jUser.setForeground(new java.awt.Color(250, 250, 250));
        jUser.setText("root");
        jUser.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        jUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jUserActionPerformed(evt);
            }
        });

        jPass.setBackground(java.awt.Color.darkGray);
        jPass.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jPass.setForeground(new java.awt.Color(250, 250, 250));
        jPass.setText("root");
        jPass.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        jPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPassActionPerformed(evt);
            }
        });

        jUserLabel.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jUserLabel.setForeground(new java.awt.Color(250, 250, 250));
        jUserLabel.setText("Brugernavn:");

        jPassLabel.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jPassLabel.setForeground(new java.awt.Color(250, 250, 250));
        jPassLabel.setText("Password:");

        jTitle.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jTitle.setForeground(new java.awt.Color(250, 250, 250));
        jTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jTitle.setText("Database login");

        jBtnDBLogin.setBackground(java.awt.Color.darkGray);
        jBtnDBLogin.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jBtnDBLogin.setForeground(new java.awt.Color(250, 250, 250));
        jBtnDBLogin.setText("Forbind");
        jBtnDBLogin.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        jBtnDBLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnDBLoginActionPerformed(evt);
            }
        });

        jLabError.setFont(new java.awt.Font("Verdana", 1, 9)); // NOI18N
        jLabError.setForeground(new java.awt.Color(180, 30, 30));
        jLabError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanPageHolderLayout = new javax.swing.GroupLayout(jPanPageHolder);
        jPanPageHolder.setLayout(jPanPageHolderLayout);
        jPanPageHolderLayout.setHorizontalGroup(
            jPanPageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jBtnDBLogin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabError, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanPageHolderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanPageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPassLabel)
                    .addComponent(jUserLabel)
                    .addComponent(jNameLabel)
                    .addComponent(jHostPortLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanPageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanPageHolderLayout.createSequentialGroup()
                        .addComponent(jHost, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabSlash)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPort, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                    .addComponent(jUser, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPass)
                    .addComponent(jName))
                .addContainerGap())
        );
        jPanPageHolderLayout.setVerticalGroup(
            jPanPageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanPageHolderLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(jPanPageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jHostPortLabel)
                    .addComponent(jLabSlash))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanPageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanPageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jUserLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanPageHolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPassLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPass))
                .addGap(18, 18, 18)
                .addComponent(jLabError, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jBtnDBLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanPageHolder, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanPageHolder, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnDBLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnDBLoginActionPerformed
        login();
    }//GEN-LAST:event_jBtnDBLoginActionPerformed

    private void jUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jUserActionPerformed
        login();
    }//GEN-LAST:event_jUserActionPerformed

    private void jPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPassActionPerformed
        login();
    }//GEN-LAST:event_jPassActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DBLogin().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnDBLogin;
    private javax.swing.JTextField jHost;
    private javax.swing.JLabel jHostPortLabel;
    private javax.swing.JLabel jLabError;
    private javax.swing.JLabel jLabSlash;
    private javax.swing.JTextField jName;
    private javax.swing.JLabel jNameLabel;
    private javax.swing.JPanel jPanPageHolder;
    private javax.swing.JTextField jPass;
    private javax.swing.JLabel jPassLabel;
    private javax.swing.JTextField jPort;
    private javax.swing.JLabel jTitle;
    private javax.swing.JTextField jUser;
    private javax.swing.JLabel jUserLabel;
    // End of variables declaration//GEN-END:variables
}
